/**
 * 
 */
package com.google.gwt.user.client.rpc.core.java.util;

/**
 * @author RAMESH
 *
 */

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.SerializationException;
import com.google.gwt.user.client.rpc.SerializationStreamReader;
import com.google.gwt.user.client.rpc.SerializationStreamWriter;

import java.util.Date;

/**
 * Custom field serializer for {@link java.util.Date}.
 */
public final class Date_CustomFieldSerializer {

  @SuppressWarnings("unused")
  public static void deserialize(SerializationStreamReader streamReader,
      Date instance) {
	  
//	  if (GWT.isClient()) {
//		  Window.alert("deserialization");
//	  }
//	  else {
//		  System.out.println("deserialization in server");
//	  }
    // No fields
  }

  public static Date instantiate(SerializationStreamReader streamReader)
      throws SerializationException {	   
	  Date d= new Date(streamReader.readLong());
//	 
//	  if (GWT.isClient()) {
//		  Window.alert("initialization:" + d.toString());
//	  }
//	  else {
//		  System.out.println("initialization in server" + d.toString());
//		  System.out.println("initialization in server" + d.getTime());
//	  }
//	  return new Date(streamReader.readInt(),streamReader.readInt(),streamReader.readInt(),streamReader.readInt(),streamReader.readInt(),streamReader.readInt());

	  if (GWT.isClient()) {
//		  System.out.println("initialization in server" + d.getTime());
		  d.applyTimeZone();
	  }

	    return d;
  }

  public static void serialize(SerializationStreamWriter streamWriter,
      Date instance) throws SerializationException {
	  
//	  if (GWT.isClient()) {
//		  Window.alert("serialization:" + instance.toString());
//		  Window.alert("serialization:" + instance.getTime());
//		  Window.alert("hours:" + instance.getHours());
//		  Window.alert("mins:" + instance.getMinutes());
//	  }
//	  else {
//		  System.out.println("serialization in server" + instance.toString());		
//	  }
	  
//	  streamWriter.writeInt(instance.getYear());
//	  streamWriter.writeInt(instance.getMonth());
//	  streamWriter.writeInt(instance.getDate());
//	  streamWriter.writeInt(instance.getHours());
//	  streamWriter.writeInt(instance.getMinutes());
//	  streamWriter.writeInt(instance.getSeconds());

	   long t = instance.getTime();
	   if (GWT.isClient()) {
		   t = t + instance.getServerConversionOffset()*60*1000  ; 
	   }
	   

//	  if (GWT.isClient()) {
//		  Window.alert("serialization:" + instance.getTime());
//	  }


	   streamWriter.writeLong(t);	  
	   
  }
}